package com.woniuxy.project115.kaer.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.mall.entity.ProductInfo;
import com.woniuxy.project115.kaer.mall.form.AddProductForm;
import com.woniuxy.project115.kaer.mall.form.FindProductForm;
import com.woniuxy.project115.kaer.mall.form.PageForm;
import com.woniuxy.project115.kaer.mall.param.AddProductParam;
import com.woniuxy.project115.kaer.mall.param.FindProductParam;
import com.woniuxy.project115.kaer.mall.param.PageParam;
import com.woniuxy.project115.kaer.mall.service.ProductInfoService;
import com.woniuxy.project115.kaer.mall.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyc
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/productInfo")
@Api(tags = "商品信息接口")
public class ProductInfoController {
    /**
     * 商品信息服务
     */
    @Resource
    private ProductInfoService productInfoService;
    //查询商品信息
    @GetMapping("/findProductByCondition")
    @ApiOperation("查询商品信息")
    public Result findProductByCondition(FindProductForm form,@RequestHeader String token){
        System.out.println(form.getPageSize());
        System.out.println(form.getPageNum());
        String role = JwtUtil.parseToken(token).get("role").toString();
        System.out.println(role);
        PageInfo pageInfo=productInfoService.findProductByCondition(BeanUtil.toBean(form, FindProductParam.class),role);
        return Result.succeed("查询成功",pageInfo);
    }
    //新增商品信息(发布商品)
    @ApiOperation("商品信息新增(发布商品)")
    @PostMapping("/addProduct")
    public Result addProduct(AddProductForm form) throws Exception {
//        System.out.println(form.getProductImage().toString());
        productInfoService.addProduct(BeanUtil.toBean(form, AddProductParam.class));
        return Result.succeed("新增成功",null);
    }
    //进货
    @ApiOperation("商品进货")
    @PostMapping("/addProductNumber")
    public Result addProductNumber( Integer productId, Integer number) throws Exception {
        productInfoService.addProductNumber(productId,number);
        return Result.succeed("进货成功",null);
    }
    //商品上架
    @ApiOperation("商品上下架")
    @PostMapping("/upProduct")
    public Result upProduct(Integer productId) throws Exception {
        productInfoService.upProduct(productId);
        return Result.succeed("修改商品状态成功",null);
    }
    //收藏或取消商品
    @ApiOperation("收藏/取消商品")
    @PostMapping("/collectProduct")
    public Result collectProduct(Integer productId, @RequestHeader("token") String token) throws Exception {
        System.out.println(token);
        Integer userId = JwtUtil.getUserId(token);
        System.out.println(userId);
        Integer n = productInfoService.collectProduct(productId, userId);
        if (n == 1) return Result.succeed("取消成功", null);
        else return Result.succeed("收藏成功", null);
    }
    //查询我的商品收藏
    @ApiOperation("查询我的商品收藏")
    @GetMapping("/selectCollect")
    public Result selectCollect(PageForm from, @RequestHeader("token") String token) throws Exception {
        PageInfo pageInfo = productInfoService.selectCollect(BeanUtil.toBean(from, PageParam.class), JwtUtil.getUserId(token));
        return Result.succeed("查询我的收藏成功", pageInfo);
    }
    @ApiOperation("查询购物车详情")
    @GetMapping("/selectShopping")
    public Result selectShopping(PageForm from, @RequestHeader("token") String token) throws Exception {
        PageInfo pageInfo = productInfoService.selectShopping(BeanUtil.toBean(from, PageParam.class), JwtUtil.getUserId(token));
        return Result.succeed("分页查询购物车成功", pageInfo);
    }

    //删除购物车商品
    @ApiOperation("删除购物车商品")
    @DeleteMapping("/delShopping")
    public Result delShopping(Integer productId, Integer number, @RequestHeader("token") String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        productInfoService.delShopping(userId, productId, number);
        return Result.succeed("删除购物车商品成功", null);
    }

    //更改购物车商品数量
    @ApiOperation("更改购物车商品数量")
    @PostMapping("/changNumber")
    public Result changNumber(Integer productId, Integer number, @RequestHeader("token") String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        productInfoService.changNumber(userId, productId, number);
        return Result.succeed("修改数量成功", null);
    }
    //查询商品收藏排行榜
    @GetMapping("/selectCollectRank")
    public Result selectCollectRank() throws Exception {
        List<ProductInfo> list = productInfoService.selectCollectRank();
        return Result.succeed("查询收藏排行榜", list);
    }
}

